package whf.shop.web;

import whf.framework.entity.AbstractEntity;
import whf.framework.entity.Entity;
import whf.framework.util.StringUtils;
import whf.framework.web.struts.WebData;
import whf.framework.web.struts.WebDispatchAction;
import whf.framework.web.struts.WebForward;
import whf.shop.entity.Currency;
import whf.shop.entity.Member;
import whf.shop.service.MemberServiceImp;
import whf.shop.utils.ShopUtils;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2007-01-11
 */
public class MemberAction extends WebDispatchAction {

	/* (non-Javadoc)
	 * @see whf.framework.web.struts.WebDispatchAction#handleBeforeCreate(whf.framework.entity.IBaseBO)
	 */
	@Override
	protected void handleBeforeCreate(Entity bo) {
		((Member)bo).setUser(ShopUtils.findBindUserForMember());
		super.handleBeforeCreate(bo);
	}

	/* (non-Javadoc)
	 * @see whf.framework.web.struts.WebDispatchAction#handleBeforeUpdate(whf.framework.entity.IBaseBO)
	 */
	@Override
	protected void handleBeforeUpdate(Entity bo) {
		((Member)bo).setUser(ShopUtils.findBindUserForMember());
		super.handleBeforeUpdate(bo);
	}

	//注册
	public WebForward prepareRegister(AbstractEntity baseForm, WebData data) throws Exception {
		baseForm.setWebMethod("register");
		return data.forward("register");
	}
	
	public WebForward register(AbstractEntity baseForm, WebData data) throws Exception {
		Member member = (Member)baseForm;
		member.setCurrency(((Currency)data.getEntityParameter(Currency.class, "currency", 0, 0)));
		member.setUser(ShopUtils.findBindUserForMember());
		MemberServiceImp.getMemberService().create(member);
		data.setAttribute(member);
		return data.forward("profile");
	}
	
	public WebForward activate(AbstractEntity baseForm, WebData data) throws Exception {
		Member member = MemberServiceImp.getMemberService().findByPrimaryKey(baseForm.getId());
		if(member.isConfirmed()){
			data.addPageMessage("Member confirmed already!");
		} else {
			member.setConfirmed(true);
			MemberServiceImp.getMemberService().update(member);
		}
		return list(baseForm, data);
	}
	
	public WebForward deactivate(AbstractEntity baseForm, WebData data) throws Exception {
		Member member = MemberServiceImp.getMemberService().findByPrimaryKey(baseForm.getId());
		if(!member.isConfirmed()){
			data.addPageMessage("Member deconfirmed already!");
		} else {
			member.setConfirmed(false);
			MemberServiceImp.getMemberService().update(member);
		}
		return list(baseForm, data);
	}
	
	public WebForward profile(AbstractEntity baseForm, WebData data) throws Exception {
		data.setAttribute(ShopUtils.getCurrentMember(data.getSession()));
		return data.forward("profile");
	}
	
	//登陆
	public WebForward prepareLogin(AbstractEntity baseForm, WebData data) throws Exception {
		baseForm.setWebMethod("login");
		return data.forward("login");
	}
	
	public WebForward login(AbstractEntity baseForm, WebData data) throws Exception {
		Member m = (Member)baseForm;
		Member member = MemberServiceImp.getMemberService().findByLogin(m.getLogin());
		if(member != null && member.isConfirmed() && StringUtils.equals(m.getPassword(), member.getPassword())){
			data.setAttribute(ShopUtils.SESSION_MEMBER, member, WebData.Scope.SESSION);
			ShopUtils.getUserContext(data.getSession());
			return data.forward("index");
		} else {
			m.setPassword("");
			if(member == null){
				data.addPageMessage("用户名不存在！");
			} else {
				if(!member.isConfirmed()){
					data.addPageMessage("会员资格尚未验证");
				} else {
					data.addPageMessage("密码错误！");
				}
			}
			return data.forward("login");
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.web.struts.WebDispatchAction#unspecified(whf.framework.entity.AbstractEntity, whf.framework.web.struts.WebData)
	 */
	@Override
	public WebForward unspecified(AbstractEntity form, WebData data) throws Exception {
		return prepareLogin(form, data);
	}
	
}